<template>
  <div class="app-container">
    <el-row :gutter="20" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Refresh" @click="getList">刷新列表</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="20" v-loading="loading">
      <el-col 
        :xs="24" :sm="12" :md="8" :lg="6" 
        v-for="item in cartList" 
        :key="item.cartItemId"
        class="mb20"
      >
        <el-card shadow="hover" class="cart-card">
          <template #header>
            <div class="card-header">
              <span class="product-name" :title="item.productName">{{ item.productName }}</span>
              <el-tag type="success" effect="dark">已添加</el-tag>
            </div>
          </template>
          
          <div class="card-content">
            <p class="product-desc">{{ item.productDescription || '暂无描述' }}</p>
            
            <div class="product-price">
              <span>单价: </span>
              <span class="price-num">¥{{ item.productPrice }}</span>
            </div>

            <div class="action-area">
              <span class="label">数量:</span>
              <el-input-number 
                v-model="item.quantity" 
                :min="1" 
                :max="item.productStock || 999" 
                size="small"
                @change="handleQuantityChange(item)"
              />
            </div>
            
            <div class="total-area">
              小计: <span class="total-price">¥{{ (item.productPrice * item.quantity).toFixed(2) }}</span>
            </div>
          </div>

          <template #footer>
            <div class="card-footer">
              <el-button 
                type="danger" 
                link 
                icon="Delete" 
                @click="handleDelete(item)"
              >移除商品</el-button>
            </div>
          </template>
        </el-card>
      </el-col>
    </el-row>

    <el-empty v-if="total === 0 && !loading" description="购物车空空如也，快去选购吧" />

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="MyCart">
import { listCart, updateCart, delCart } from "@/api/shopping/myCart";
import { getCurrentInstance, onMounted, reactive, ref, toRefs } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();

const loading = ref(true);
const cartList = ref([]);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 12, // 设置为4的倍数，视觉效果更好
  }
});

const { queryParams } = toRefs(data);

/** 查询购物车列表 */
function getList() {
  loading.value = true;
  listCart(queryParams.value).then(response => {
    cartList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 处理数量变更 */
function handleQuantityChange(item) {
  // 防抖或直接提交
  updateCart({
    cartItemId: item.cartItemId,
    quantity: item.quantity,
    productId: item.productId // 如果后端逻辑需要
  }).then(() => {
    // 静默更新成功，或者轻提示
    // ElMessage.success("数量已更新");
  }).catch(() => {
    getList(); // 失败则回滚界面数据
  });
}

/** 删除操作 */
function handleDelete(item) {
  ElMessageBox.confirm('确认将 "' + item.productName + '" 移出购物车吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    return delCart(item.cartItemId);
  }).then(() => {
    getList();
    ElMessage.success("移除成功");
  }).catch(() => {});
}

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.mb20 {
  margin-bottom: 20px;
}

.cart-card {
  height: 100%;
  display: flex;
  flex-direction: column;
  
  :deep(.el-card__body) {
    flex: 1;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  .product-name {
    font-weight: bold;
    font-size: 16px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 70%;
  }
}

.card-content {
  .product-desc {
    font-size: 13px;
    color: #999;
    margin-bottom: 15px;
    height: 40px; /* 限制高度，保持卡片对齐 */
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  
  .product-price {
    margin-bottom: 10px;
    font-size: 14px;
    .price-num {
      color: #f56c6c;
      font-weight: bold;
      font-size: 16px;
    }
  }

  .action-area {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    
    .label {
      margin-right: 10px;
      font-size: 14px;
    }
  }
  
  .total-area {
    margin-top: 15px;
    border-top: 1px dashed #eee;
    padding-top: 10px;
    font-size: 14px;
    text-align: right;
    .total-price {
      color: #f56c6c;
      font-weight: bold;
      font-size: 18px;
    }
  }
}

.card-footer {
  text-align: right;
  padding-top: 10px; 
}
</style>